package com.mz.ozy.widget.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.mz.ozy.R;
import com.mz.ozy.adapter.SpecTagAdapter;
import com.mz.ozy.bean.ShopSpecListBean;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.SpecVipWindow;
import com.mz.ozy.widget.flowlayout.FlowLayout;
import com.mz.ozy.widget.flowlayout.TagFlowLayout;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author crazyZhangxl on 2018/10/25.
 * Describe:
 */

public class VipSpecDialog extends DialogFragment implements View.OnClickListener {
    private ImageView goodsImage;
    private TextView tvGoodsPrice;
    private TextView tvGoodsNum;
    private TextView numberTv;
    private TextView tv_confirm;
    private TextView joinCarTv;
    private int number = 1;
    private VipSpecDialog.OnClickListener mOnClickListener;
    private ShopSpecListBean mSpecListBeans;
    private SpecTagAdapter mSearchTagAdapter;
    private int ggId;
    private String name;
    private final int mOpType;
    private int mPosition;
    private Context mSpecContext;

    public VipSpecDialog(Context context, ShopSpecListBean specListBeans, int opType, int position, VipSpecDialog.OnClickListener mOnClickListener) {
        this.mSpecContext = context;
        this.mSpecListBeans = specListBeans;
        if (mSpecListBeans == null) {
            mSpecListBeans = new ShopSpecListBean();
        }
        this.mOpType = opType;
        this.mPosition = position;
        this.mOnClickListener = mOnClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View rootView = inflater.inflate(R.layout.pop_vip_spec, container, false);
        init(rootView);
        return rootView;
    }

    private void init(View rootView) {
        goodsImage = rootView.findViewById(R.id.shopImage);
        tvGoodsPrice = rootView.findViewById(R.id.tv_goods_price);
        tvGoodsNum = rootView.findViewById(R.id.tv_goods_num);
        numberTv = rootView.findViewById(R.id.numberTv);
        tv_confirm = rootView.findViewById(R.id.tv_confirm);
        joinCarTv = rootView.findViewById(R.id.joinCarTv);

        TextView lessTv = rootView.findViewById(R.id.lessTv);
        TextView addTv = rootView.findViewById(R.id.addTv);

        if (mOpType != 2) {
            joinCarTv.setVisibility(View.GONE);
            tv_confirm.setText("确定");
        }
        tvGoodsPrice.setText(mSpecListBeans.getPrice());

        tv_confirm.setOnClickListener(this);
        joinCarTv.setOnClickListener(this);
//        rootView.findViewById(R.id.buyNowTv).setOnClickListener(this);
        View view = rootView.findViewById(R.id.view);
        ImageView ivGoodSpecClose = rootView.findViewById(R.id.iv_goods_spec_close);
        view.setOnClickListener(this);
        ivGoodSpecClose.setOnClickListener(this);
        if (mSpecListBeans.isOperate()) {
            lessTv.setOnClickListener(this);
            addTv.setOnClickListener(this);
        }
        TagFlowLayout mFlowTagLayout = rootView.findViewById(R.id.flowTagLayout);
        final String goodsImg = mSpecListBeans.getGoodsImg();
        if (!TextUtils.isEmpty(goodsImg)) {
            GlideUtils.loadRoundImg(mSpecContext, goodsImage, mSpecListBeans.getGoodsImg(), R.mipmap.default_foods_icon, 6);
        } else {
            GlideUtils.loadRoundImg(mSpecContext, goodsImage, R.mipmap.default_foods_icon, 6);
        }
        final List<ShopSpecListBean.ShopSpecBean> specBeanList = mSpecListBeans.getList();
        if (specBeanList != null) {
            if (specBeanList.size() > 0) {
                ggId = specBeanList.get(mPosition).getId();
                name = specBeanList.get(mPosition).getName();
                tvGoodsNum.setText(mSpecContext.getString(R.string.in_stock_format, specBeanList.get(mPosition).getStock() + ""));
            }
            mSearchTagAdapter = new SpecTagAdapter(mSpecContext, mFlowTagLayout, specBeanList, mPosition);
            mFlowTagLayout.setAdapter(mSearchTagAdapter);
            mFlowTagLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                @Override
                public boolean onTagClick(View view, int position, FlowLayout parent) {
                    mSearchTagAdapter.setSelectPosition(position);
                    final ShopSpecListBean.ShopSpecBean shopSpecBean = specBeanList.get(position);
                    mPosition = position;
                    ggId = shopSpecBean.getId();
                    name = shopSpecBean.getName();
//                    tvGoodsPrice.setText(getPrice(shopSpecBean));
                    tvGoodsNum.setText(mSpecContext.getString(R.string.in_stock_format, shopSpecBean.getStock() + ""));
                    return false;
                }
            });
        }
    }

    @NotNull
    private String getPrice(ShopSpecListBean.ShopSpecBean bean) {
        String price = "";
        if (!TextUtils.isEmpty(bean.getPrice()) && Double.parseDouble(bean.getPrice()) != 0.00) {
            price += "¥";
            price += bean.getPrice();
        }
        if (!TextUtils.isEmpty(bean.getMarket_price()) && Double.parseDouble(bean.getMarket_price()) != 0.00) {
            if (!TextUtils.isEmpty(price)) {
                price += "+";
                price += bean.getMarket_price();
                price += "积分";
            } else {
                price += bean.getMarket_price();
                price += "积分";
            }
        }
        if (TextUtils.isEmpty(price)) {
            price = "¥0.00";
        }
        return price;
    }

    @Override
    public void onStart() {
        super.onStart();

        Window window = getDialog().getWindow();
        if (window != null) {
            // 一定要设置Background，如果不设置，window属性设置无效
            window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
            DisplayMetrics dm = new DisplayMetrics();
            if (getActivity() != null) {
                WindowManager windowManager = getActivity().getWindowManager();
                if (windowManager != null) {
                    windowManager.getDefaultDisplay().getMetrics(dm);
                    WindowManager.LayoutParams params = window.getAttributes();
                    params.gravity = Gravity.BOTTOM;
                    // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
//                    params.width = ViewGroup.LayoutParams.MATCH_PARENT;
//                    params.height = height;
                    params.width = dm.widthPixels; //设置宽度
                    params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
//                            dm.heightPixels; //设置高度
                    window.setAttributes(params);
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_goods_spec_close:
            case R.id.view:
                mOnClickListener.onSubmit(ggId, name, 2, mPosition);
                dismiss();
                break;
            case R.id.tv_confirm:
                if (mOpType == 2 || mOpType == 1) {
                    mOnClickListener.onSubmit(ggId, name, 1, mPosition);
                } else {
                    mOnClickListener.onSubmit(ggId, name, 0, mPosition);
                }
                dismiss();
                break;
            case R.id.joinCarTv:
                mOnClickListener.onSubmit(ggId, name, 0, mPosition);
                dismiss();
                break;
            case R.id.lessTv:
                if (number > 1) {
                    number--;
                }
                numberTv.setText(number + "");
                mOnClickListener.updateNumber(number);
                break;
            case R.id.addTv:
                number++;
                numberTv.setText(number + "");
                mOnClickListener.updateNumber(number);
                break;


        }

    }

    public interface OnClickListener {
        void onSubmit(int ggId, String name, int type, int position);

        void updateNumber(int number);

    }
}

